#include "pch.h"
#include "Foundation.h"
namespace mini
{
	namespace foundation
	{
		const std::string getExterName(const std::string& strName)
		{
			if (std::string::npos == strName.rfind("."))
			{
				return "";
			}
			else
			{
				return strName.substr(strName.rfind(".") + 1);
			}
		}

		std::string getFileExtension(const std::string& name)
		{
			std::string strExtension = "";
			std::vector<std::string>&& result = splitString<std::string>(name, ".");
			if (result.size() > 1)
			{
				strExtension = result[1];
				std::transform(strExtension.begin(), strExtension.end(), strExtension.begin(), ::tolower);
			}
			return strExtension;
		}

		std::string getFile(const std::string& name)
		{
			std::string strExtension = "";
			std::vector<std::string>&& result = splitString<std::string>(name, ".");
			if (result.size() > 1)
			{
				strExtension = result[0];
				std::transform(strExtension.begin(), strExtension.end(), strExtension.begin(), ::tolower);
			}
			return strExtension;
		}

		std::string getFilePath(const std::string& strName)
		{
			auto size = strName.rfind('\\');
			if (std::string::npos == size)
			{
				size = strName.rfind('/');
				if (std::string::npos != size)
				{
					return strName.substr(0, size + 1);
				}
			}
			else
			{
				return strName.substr(0, size + 1);
			}
			return "";
		}

		std::tuple< std::vector<std::string>, std::vector<std::string> >
			getFileInfoList(std::string&& stringDir)
		{
			_finddata_t fileData;
			std::vector<std::string> strFileDir;
			std::vector<std::string> strFileName;
			long handle = 0;
			stringDir.append("/*.*");
			handle = _findfirst(stringDir.c_str(), &fileData);
			if (handle != -1)
			{
				std::string filterString[2] = { {".."}, {"."} };
				auto filter = [&](const std::string&& str)
				{
					for (auto& item : filterString)
					{
						if (item == str)
						{
							return;
						}
					}
					strFileDir.push_back(str);
				};
				do
				{
					if (fileData.attrib & _A_SUBDIR)
					{
						filter(fileData.name);
					}
					else
					{
						strFileName.push_back(fileData.name);
					}
				} while (_findnext(handle, &fileData) == 0);
				_findclose(handle);
			}
			std::tuple< std::vector<std::string>, std::vector<std::string> > result(strFileDir, strFileName);
			return result;
		}
	}
}

